<template>
  <div>
    <van-nav-bar
      :title="msg"
      left-text="返回"
      left-arrow
      @click-left="goBackFn"
    />
    <van-field
      v-model="username"
      clearable
      required
      label="用户名"
      placeholder="请输入用户名"
      :error-message="useErr"
    />
    <van-field
      v-model="password"
      type="password"
      clearable
      required
      label="密码"
      placeholder="请输入密码"
      :error-message="passErr"
    />

    <van-button type="primary" @click="userLoginFn">登录</van-button>
    <van-button type="primary" @click="registerBtn">注册</van-button>

    <footerBar></footerBar>
  </div>
</template>
<script>
import axios from "axios";
import API_LIST from "@/APIList.config";
import footerBar from "./footerBar";

export default {
  name: "userLogin",
  data() {
    return {
      msg: "登录页面",
      username: "",
      password: "",
      password2: "",
      description: "",
      useErr: "",
      passErr: "",
      passErr2: "",
    };
  },
  components: { footerBar },

  methods: {
    goBackFn() {
      this.$router.go(-1);
    },
    userLoginFn() {
      let _loginObj = {
        username: this.username,
        password: this.password,
      };
      axios.post(API_LIST.login_post, _loginObj).then((_d) => {
        this.$dialog
          .alert({
            message: "欢迎您！" + _d.data.regsInfo.username + "登录成功",
          })
          .then(() => {
            if (_d.data.regs_code === 3) {
              localStorage.userName = _d.data.regsInfo.username;
              localStorage.user = JSON.stringify(_d.data.regsInfo);
            }
            this.$router.push({ path: "/" }, () => {
              localStorage.setItem("inxState", 0);
            });
          });
      });
    },
    registerBtn() {
      this.$router.push({ path: "/register" });
    },
  },
};
</script>